package com.example.service.impl;

import com.example.entity.Books;
import com.example.entity.Role;
import com.example.entity.Student;
import com.example.entity.User;
import com.example.mapper.StudentMapper;
import com.example.mapper.UserMapper;
import com.example.service.StudentService;
import com.example.utils.File.ExcelUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author： wzz
 * @date： 2021-07-28 18:14
 */

@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;
    @Resource
    private UserMapper userMapper;


    @Override
    public List<Books> GetStuBook(String stu_num) {
        return studentMapper.GetStuBook(stu_num);
    }

    @Override
    public Integer addStu(Student student) {
        userMapper.AddUser(student.getNum(),2);
        return  studentMapper.AddStudent(student);
    }

    @Override
    public int updateLoginTime(String loginTime, String studentNumber) {
        return studentMapper.updateLoginTime(loginTime, studentNumber);
    }


    @Override
    public List<Student> queryAll() {
        List<Student> studentList =  studentMapper.queryAll();
        for(Student stu:studentList){
            Role role = new Role();
            role.setROLE_id(1);
            role.setROLE_name("ROLE_student");
            stu.setRole(role);
        }
        return studentList;
    }

    @Override
    public int importStudent(MultipartFile file) throws IOException {
        int result = 0;
        List<User> userList = new ArrayList<>();
        List<Student> studentList= ExcelUtil.readStuExcel(file);
        for (Student student:studentList) {
            student.setPassword(student.getNum());
            userList.add(new User(student.getNum(),new Role("ROLE_student",2)));
        }
        userMapper.ImportUsers(userList);
        result = studentMapper.importStudent(studentList);
        return result;
    }

    @Override
    public List<String> queryClasses() {
        return studentMapper.queryClasses();
    }

    @Override
    public List<Student> conditionSearch(String studentNumber, String name, String classes) {
        return studentMapper.conditionSearch(studentNumber, name, classes);
    }

    @Override
    public Student queryOne(String studentNumber) {
        Student student =studentMapper.queryOne(studentNumber);
        student.setRole(new Role("ROLE_student",2));
        return student;
    }

    @Override
    public int updateStudent(Student student) {
        return studentMapper.updateStudent(student);
    }

    @Override
    public int delStudent(String studentNumber) {
        return studentMapper.delStudent(studentNumber);
    }

    @Override
    public List<String> getCla(String jobNumber) {
        return studentMapper.getCla(jobNumber);
    }

    @Override
    public List<Student> queryMy(String num) {
        List<Student> studentList =  studentMapper.queryMy(num);
        for(Student stu:studentList){
            Role role = new Role();
            role.setROLE_id(1);
            role.setROLE_name("ROLE_student");
            stu.setRole(role);
        }
        return studentList;
    }

    @Override
    public List<String> queryMyClass(String num) {

        return studentMapper.queryMyClass(num);
    }
}
